Skip to content

Introduce SSLClient library based on WiFiClientSecure #6055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from

Conversation

sgalabov
Copy link

Summary

This PR reworks WiFiClientSecure into SSLClient so that it can be used with any Client-derived class.
The PR was tested with ESP32 WiFi as well as with a GSM module supported by TinyGSM.

Impact

Any Client-derived class can get SSL applied to it, e.g. WiFiClient, but also TinyGsmClient and any other clients.
Protocol Clients can then be put on top of SSL, e.g. PubSubClient or ArduinoHttpClient.
This may also allow for DTLS to be supported as well in the same style in the future, e.g. CoAP over both WiFi and GSM, etc.

@sgalabov sgalabov changed the title Ssl client Introduce SSLClient library based on WiFiClientSecure Dec 22, 2021
@sgalabov
Copy link
Author

seems to build fine with the last commit (8b29310)

@gonzabrusco
Copy link
Contributor

This looks promising.

@torntrousers
Copy link
Contributor

torntrousers commented Mar 25, 2022

Looks interesting to me too. Have you any ideas if it can also work with the mbedtls integration with the Microchip ATECC608 secure element which is presently only available with esp-idf?
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/secure_element.html

@torntrousers
Copy link
Contributor

I've been trying this and it works well for me with TinyGSM. Would be great if it could be merged. @me-no-dev ?

@me-no-dev
Copy link
Member

yup :) in a bit. Wanted to merge S3 support first.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 28, 2022

Unit Test Results

0 files  0 suites   0s ⏱️
0 tests 0 ✔️ 0 💤 0

Results for commit d75852b.

♻️ This comment has been updated with latest results.

@me-no-dev
Copy link
Member

There seem to be some issues with S3. I'll have a look. So this seems like a new implementation to replace WiFiClientSecure? Why not modify the original class instead? Having two classes that do the same thing (for the majority of users) will be confusing.

@igrr
Copy link
Member

igrr commented Mar 28, 2022

I think if WiFiClientSecure could be implemented as a thin wrapper over SSLClient, this would be a nice approach.

First, we would still have just one implementation (SSLClient).
Second, both the old code which uses WiFiClientSecure and the new code with SSLClient would continue to work.

However to achieve this we have to ensure that SSLClient behaves the same way as WiFiClientSecure, or that a backwards-compatible WiFiClientSecure wrapper would be possible to write.

Another point, I think this decision should be synchronized with esp8266/Arduino project. It wouldn't be great to introduce incompatibilities between the two cores intentionally.

@igrr
Copy link
Member

igrr commented Mar 28, 2022

By the way, there is also https://github.com/OPEnSLab-OSU/SSLClient library which works with many official Arduino boards. @sgalabov do you know of this library? Is choosing the same name intentional?

@sgalabov
Copy link
Author

sgalabov commented Mar 28, 2022

@igrr, @me-no-dev, my needs were to be able to use SSL on top of both WiFi and GSM (via TinyGSM), so when I had a closer look at WiFiClientSecure it was logical for me to try and rework it into something that could work with any Client based class and yes, the idea was to go on and convert WiFiClientSecure to work with SSLClient on top of WiFiClient, but it turned out I had to change HTTPClient as well and I just haven't been able to find the time to do that yet (and don't know when/whether I'll be able to, to be honest)... other than that I basically used WiFiClientSecure as a base, including its README and its examples :-)

@igrr, I didn't know about https://github.com/OPEnSLab-OSU/SSLClient so the name is more coincidental than anything else - just thought it fitted in well with its purpose :-)

In the meantime someone posted (although I can't find the message here) about https://github.com/govorox/SSLClient which seems to be more or less the same approach and idea as what I have submitted. I wish I had known about this before - maybe it would have saved me a bit of work...

I thought about the correct place to have such a lib and my opinion at the time was that it belongs with the Arduino support for ESP32 as this is the main target for the lib and would be a logical place to look for something like this for others :-)
Also, I am not sure I will have time to support something like this as open source and would much rather see it integrated in something bigger and with a well established community, which is why I have submitted it here.

@gonzabrusco
Copy link
Contributor

gonzabrusco commented Mar 28, 2022

There seem to be some issues with S3. I'll have a look. So this seems like a new implementation to replace WiFiClientSecure? Why not modify the original class instead? Having two classes that do the same thing (for the majority of users) will be confusing.

WiFiClient wificlient;
SSLClient  client(wificlient); <--- this would be our new WiFiClientSecure

Maybe WiFiClientSecure could be just a wrapper of SSLClient(WiFiClient). Just for backwards compatibility.

@gonzabrusco
Copy link
Contributor

For this to be complete as @sgalabov said, HTTPClient should use Client instead of WiFiClient (like the standard ArduinoHttpClient library). That way you could use an SSLClient on top of a WiFiClient in the included HTTPClient from this repository.

@gonzabrusco
Copy link
Contributor

SSLClient.zip

This would be the same @sgalabov uploaded but with the latest WiFiSecureClient changes incorporated (specifically, certificate bundle).

The only example that does not work is the one relying on HTTPClient because it was developed to work with WiFiClient and not with Client

@sgalabov
Copy link
Author

It would probably be worth it to have a discussion on where and when this needs to go and then take the time to rework it based on the current WiFiClientSecure and rework everything else that needs to be touched, so that WiFiClientSecure becomes equivalent to SSLClient on top of WiFiClient and HTTPClient (at least) is taught about that. Probably SSLClient can be part of the WiFiClientSecure lib, although I personally tend to prefer them to be separate as it would be more visible and understandable what each one does...
What do you guys think?

@torntrousers
Copy link
Contributor

Sounds good to me. I'm doing some more testing with SSLClient this week as it looks like there might be a subtle difference it the way it behaves compared to WiFiClientSecure which I'm trying to get to the bottom of.

@me-no-dev
Copy link
Member

@sgalabov I have fixed and updated your branch. Please pull the changes on your end.

@sgalabov
Copy link
Author

@sgalabov I have fixed and updated your branch. Please pull the changes on your end.

not sure what you mean - I can already see your changes in my branch :-)

@me-no-dev
Copy link
Member

@sgalabov I have fixed and updated your branch. Please pull the changes on your end.

not sure what you mean - I can already see your changes in my branch :-)

I guess your git client already pulled them (mine does too)

@torntrousers
Copy link
Contributor

I'm doing some more testing with SSLClient this week as it looks like there might be a subtle difference it the way it behaves compared to WiFiClientSecure which I'm trying to get to the bottom of.

What this looks like is that when an SSLClient.connect fails then SSLClient calls stop() which frees up all the resources and you can't use that SSLClient instance again and if you try it fails "Client not initialized" from line 91 in ssl_client.cpp, whereas with WifiClientSecure you can reuse it after a failed connection.

@gonzabrusco
Copy link
Contributor

gonzabrusco commented Mar 31, 2022

What this looks like is that when an SSLClient.connect fails then SSLClient calls stop() which frees up all the resources and you can't use that SSLClient instance again and if you try it fails "Client not initialized" from line 91 in ssl_client.cpp, whereas with WifiClientSecure you can reuse it after a failed connection.

Yes the behaviour is not the same. Because when you create an SSLClient, you pass the client for the connection. But when you stop() the SSLClient, it clears the passed client internally (in stop_ssl_socket() you will see ssl_client->client = nullptr; at the end). In WiFiClientSecure this worked because it didn't use clients, it used sockets and created the connections itself. But SSLClient needs a new client to work with if you stop it. Your best bet with this library as is, it to delete the SSLClient and recreate a new one.

Not sure if this is the best approach, but it is what it is haha. This SSLClient idea is great but it needs more work.

EDIT:
Something like this could be better so that SSLClient does not looses its client on stop().

// save only interesting field
int timeout = ssl_client->handshake_timeout;
Client *client = ssl_client->client;
// reset embedded pointers to zero
memset(ssl_client, 0, sizeof(sslclient_context));
// restore intesting fields
ssl_client->client = client;
ssl_client->handshake_timeout = timeout;

@torntrousers
Copy link
Contributor

Another thing, SSLClient doesn't initialse _use_insecure, it should have bool _use_insecure = false; in SSLClient.h, otherwise the first requests i do get "WARNING: Skipping SSL Verification. INSECURE!".

@CLAassistant
Copy link

CLAassistant commented May 6, 2023

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ lucasssvaz
❌ sgalabov
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Contributor

github-actions bot commented Jan 29, 2024

Warnings
⚠️

Some issues found for the commit messages in this PR:

  • the commit message "Amend CMakeLists.txt to include SSLClient lib":
    • summary looks empty
    • type/action looks empty
  • the commit message "Fix build failure due to change in MbedTLS defines":
    • summary looks empty
    • type/action looks empty
  • the commit message "Remove non-working example for now":
    • summary looks empty
    • type/action looks empty
  • the commit message "Rework WiFiClientSecure into SSLClient which works with any Client-derived class. Tested with ESP32 WiFiClient and TinyGsmClient.":
    • summary should not end with a period (full stop)
    • summary looks empty
    • type/action looks empty

Please fix these commit messages - here are some basic tips:

  • follow Conventional Commits style
  • correct format of commit message should be: <type/action>(<scope/component>): <summary>, for example fix(esp32): Fixed startup timeout issue
  • allowed types are: change,ci,docs,feat,fix,refactor,remove,revert,test
  • sufficiently descriptive message summary should be between 20 to 72 characters and start with upper case letter
  • avoid Jira references in commit messages (unavailable/irrelevant for our customers)

TIP: Install pre-commit hooks and run this check when committing (uses the Conventional Precommit Linter).

⚠️ Please consider squashing your 8 commits (simplifying branch history).
⚠️

The source branch "SSLClient" incorrect format:

  • contains uppercase letters. This can cause troubles on case-insensitive file systems (macOS).
    Please rename your branch.
Messages
📖 This PR seems to be quite large (total lines of code: 1481), you might consider splitting it into smaller PRs

👋 Hello sgalabov, we appreciate your contribution to this project!


Click to see more instructions ...


This automated output is generated by the PR linter DangerJS, which checks if your Pull Request meets the project's requirements and helps you fix potential issues.

DangerJS is triggered with each push event to a Pull Request and modify the contents of this comment.

Please consider the following:
- Danger mainly focuses on the PR structure and formatting and can't understand the meaning behind your code or changes.
- Danger is not a substitute for human code reviews; it's still important to request a code review from your colleagues.
- Resolve all warnings (⚠️ ) before requesting a review from human reviewers - they will appreciate it.
- Addressing info messages (📖) is strongly recommended; they're less critical but valuable.
- To manually retry these Danger checks, please navigate to the Actions tab and re-run last Danger workflow.

Review and merge process you can expect ...


We do welcome contributions in the form of bug reports, feature requests and pull requests.

1. An internal issue has been created for the PR, we assign it to the relevant engineer.
2. They review the PR and either approve it or ask you for changes or clarifications.
3. Once the GitHub PR is approved we do the final review, collect approvals from core owners and make sure all the automated tests are passing.
- At this point we may do some adjustments to the proposed change, or extend it by adding tests or documentation.
4. If the change is approved and passes the tests it is merged into the default branch.

Generated by 🚫 dangerJS against f26f08d

@lucasssvaz
Copy link
Collaborator

@sgalabov Would it be possible to update this PR to 3.0.0 ?

@VojtechBartoska VojtechBartoska added the Status: Review needed Issue or PR is awaiting review label Jan 30, 2024
@VojtechBartoska VojtechBartoska added this to the 3.0.0-RC1 milestone Jan 30, 2024
@VojtechBartoska
Copy link
Contributor

@sgalabov will you be able to update this PR? Also please sign CLA. Thanks

@sgalabov sgalabov closed this Feb 5, 2024
@sgalabov
Copy link
Author

sgalabov commented Feb 5, 2024

Sorry, I won't be able to do that due to other commitments... I had forgotten about this PR and have no time to come back to it at the moment, so I am closing it.

@sgalabov sgalabov deleted the SSLClient branch February 5, 2024 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Review needed Issue or PR is awaiting review
Projects
Development

Successfully merging this pull request may close these issues.

9 participants